package com.xiaoshuidi.cloud.module.contract.service.auth.impl;

import com.alibaba.fastjson.JSONObject;
import com.fadada.sdk.base.model.req.RegisterAccountParams;
import com.fadada.sdk.verify.model.req.PersonVerifyUrlParams;
import com.xiaoshuidi.cloud.framework.common.util.http.OkHttpClientUtil;
import com.xiaoshuidi.cloud.framework.fadada.core.FadadaAuthServiceApi;
import com.xiaoshuidi.cloud.framework.fadada.core.enums.AccountTypeEnum;
import com.xiaoshuidi.cloud.framework.fadada.core.enums.IdentTypeEnum;
import com.xiaoshuidi.cloud.framework.fadada.core.enums.PageModifyEnum;
import com.xiaoshuidi.cloud.framework.fadada.core.enums.VerifiedWayEnum;
import com.xiaoshuidi.cloud.framework.fadada.core.service.dto.VerifyUrlRes;
import com.xiaoshuidi.cloud.module.contract.controller.app.contract.vo.AuthApplyReqVO;
import com.xiaoshuidi.cloud.module.contract.enums.ErrorCodeConstants;
import com.xiaoshuidi.cloud.module.contract.service.auth.UserAuthService;
import com.xiaoshuidi.cloud.module.contract.service.auth.dto.TenantGetUserInfoDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static com.xiaoshuidi.cloud.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * @Description:
 * @author: zhaoyu
 * @date: 2023/4/25
 */
@Service
@Validated
@Slf4j
@RefreshScope
public class UserAuthServiceImpl implements UserAuthService {

//    @Value("${fadada.open_id_prefix}")
//    private String OPEN_ID_PREFIX;
//    @Value("${fadada.backend_callback_url}")
//    private String BACKEND_CALLBACK_URL;
//    @Value("${fadada.frontend_callback_url}")
//    private String FRONTEND_CALLBACK_URL;
//
//    @Value("${tenant.host}")
//    private String TENANT_HOST;
//
//    @Value("${tenant.get_user_info}")
//    private String TENANT_GET_USER_INFO;
//
//    @Value("${caspain.host}")
//    private String CASPAIN_HOST;
//
//    @Value("${caspain.auth_fadada}")
//    private String CASPAIN_AUTH_FADADA;
    @Resource
    private FadadaAuthServiceApi fadadaAuthServiceApi;

//    @Override
    public String applyFaceAuth(AuthApplyReqVO reqVO) {
//        //1、调用tenant服务查询用户实名状态
//        TenantGetUserInfoDTO userInfoDTO;
//        try {
//            Map<String, String> headers = new HashMap<>();
//            headers.put("Authorization",reqVO.getPhone());
//            Map<String, String> querys = new HashMap<>();
//            querys.put("owner_user_id",reqVO.getOwnerUserId());
//            String responseStr = OkHttpClientUtil.doGet(TENANT_HOST, TENANT_GET_USER_INFO, headers, querys);
//            userInfoDTO = JSONObject.parseObject(responseStr, TenantGetUserInfoDTO.class);
//        } catch (Exception e) {
//            throw exception(ErrorCodeConstants.USER_INFORMATION_QUERY_FAILED);
//        }
//        //2、判断实名状态
//        if (Objects.nonNull(userInfoDTO) && userInfoDTO.getData().getPage_info().isAuthentification_user()){
//            throw exception(ErrorCodeConstants.USER_HAS_BEEN_SUCCESSFULLY_AUTHENTICATED);
//        }
//
//        //3、注册法大大账号
//        RegisterAccountParams params = new RegisterAccountParams();
//        String openId = OPEN_ID_PREFIX + reqVO.getCard();
//        params.setOpenId(openId);
//        params.setAccountType(AccountTypeEnum.PERSONAL.getCode());
//        String customerId = fadadaAuthServiceApi.accountRegister(params);
//        //4、获取个人实名认证地址 返回前端地址等数据
//        String url = "";
//        if (StringUtils.isNotBlank(customerId)){
//            PersonVerifyUrlParams verifyUrlParams = new PersonVerifyUrlParams();
//            verifyUrlParams.setCustomerId(customerId); //客户编号
//            verifyUrlParams.setVerifiedWay(VerifiedWayEnum.FACE_RECOGNITION.getCode()); //实名认证套餐类型
//            verifyUrlParams.setPageModify(PageModifyEnum.ALLOW.getCode()); //是否允许用户页面修改 1允许 2不允许
//            verifyUrlParams.setNotifyUrl(BACKEND_CALLBACK_URL); //异步回调地址
//            verifyUrlParams.setReturnUrl(FRONTEND_CALLBACK_URL); //同步通知url
//            verifyUrlParams.setCustomerName(reqVO.getName()); //姓名
//            verifyUrlParams.setCustomerIdentType(IdentTypeEnum.ID_NO.getCode()); //证件类型
//            verifyUrlParams.setCustomerIdentNo(reqVO.getCard()); //证件号码
//            VerifyUrlRes personVerifyUrl = fadadaAuthServiceApi.getPersonVerifyUrl(verifyUrlParams);
//            if (Objects.nonNull(personVerifyUrl)){
//                url = personVerifyUrl.getUrl();
//                //5、新增或更新法大大实名基本信息
//                try {
//                    Map<String,String> queryForm = new HashMap<>();
//                    queryForm.put("name",reqVO.getName());
//                    queryForm.put("id_card",reqVO.getCard());
//                    queryForm.put("phone",reqVO.getPhone());
//                    queryForm.put("signature_code",customerId);
//                    queryForm.put("transaction_no",personVerifyUrl.getTransactionNo());
//                    queryForm.put("fadada_id",openId);
//                    queryForm.put("auth_type","2");
//                    String authFadadaResStr = OkHttpClientUtil.doPost(CASPAIN_HOST + CASPAIN_AUTH_FADADA, null, queryForm);
//                    log.info("新增或更新法大大实名基本信息返回值：{}", authFadadaResStr);
//                } catch (Exception e) {
//                    throw exception(ErrorCodeConstants.USER_AUTH_INFO_INSERT_FAILED);
//                }
//                return url;
//            }
//        }
        return null;
    }



}
